package com.sc.VenusQueryBeta.presenter.account.cundan;


import com.hengjun.kingstar.sdk.utils.LogUtils;
import com.sc.VenusQueryBeta.contract.account.cundan.CunDanContract;
import com.sc.VenusQueryBeta.model.account.cundan.CunDanModel;
import com.sc.VenusQueryBeta.model.bean.account.CunDanBean;
import com.sc.VenusQueryBeta.model.bean.account.CurrencyListBean;

import io.reactivex.functions.Consumer;

/**
 * Created by SC-02 on 2018/9/19.
 * 存單明细presenter 继承契约类的presenper
 */
public class CunDanPresenter extends CunDanContract.CunDanPresenter {
    @Override
    protected CunDanContract.ICunDanModel getModel() {
        return CunDanModel.newInstance();
    }

    /**
     * 查询币种
     *
     * @param serverAddress
     * @param projectName
     * @param serverPort
     */
    @Override
    public void queryAllCurrencyList(String serverAddress, String projectName, int serverPort) {
        if (mRxManager != null && mIModel != null) {
            mRxManager.register(mIModel.queryAllCurrencyList(serverAddress, projectName, serverPort).subscribe(new Consumer<CurrencyListBean>() {
                @Override
                public void accept(CurrencyListBean currencyListBean) throws Exception {
                    mIView.showqueryAllCurrencyList(currencyListBean);
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    LogUtils.e("Network error." + throwable.getMessage());
//                if (mIView != null)
//                    return;
                    mIView.showToast("Network error.");
                    mIView.showNetworkError(throwable);
                }
            }));
        }
    }

    @Override
    public void onStart() {

    }

    /**
     * 實例化對象
     */
    public static CunDanPresenter newInstance() {
        return new CunDanPresenter();
    }

    @Override
    public void cundan(int clientindex, String serverAddress, String projectName, int serverPort,String currencynameCN) {
        if (mIView == null || mIModel == null)
            return;
        mRxManager.register(mIModel.cundan(clientindex, serverAddress, projectName, serverPort ,currencynameCN).subscribe(new Consumer<CunDanBean>() {
            @Override
            public void accept(CunDanBean cunDanBean) throws Exception {
                if (mIView != null)
                    mIView.showcundanDataSuccess(cunDanBean);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if (mIView != null) {
//                    if (mIView.isVisiable())
                    LogUtils.e("錯愕error==" + throwable.getMessage());
                    mIView.showToast("Network error");
                    mIView.showNetworkError(throwable);
                }
            }
        }));
    }
}
